﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using Lenovo.CFI.Common.Dic;
using Lenovo.CFI.Web.Helper;
using Lenovo.CFI.BLL.DicMgr;
using Lenovo.CFI.Common;

namespace Lenovo.CFI.Web.VP
{
    public partial class CfgData : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.ViewState["DicName"] = 4;

                this.BindData();

                MessageHelper.Prepare(this.Page);
            }
        }

        private void BindData()
        {
            DictionaryName dic = (DictionaryName)this.ViewState["DicName"];
            SetEditArea(dic);
            switch (dic)
            {
                case DictionaryName.Keyword:
                    this.GvList.DataSource = KeywordDicMgr.Get(true);
                    break;
                case DictionaryName.OtherBu:
                    this.GvList.DataSource = OtherBuDicMgr.Get(true);
                    break;
                case DictionaryName.IdeaType:
                    this.GvList.DataSource = IdeaTypeDicMgr.Get(true);
                    break;
                case DictionaryName.NextAction:
                    this.GvList.DataSource = NextActionDicMgr.Get(true);
                    break;
                case DictionaryName.TopicType:
                    this.GvList.DataSource = TopicTypeDicMgr.Get(true);
                    break;
            }

            this.GvList.DataBind();
        }

        private void SetEditArea(DictionaryName dic)
        {
            switch (dic)
            {
                case DictionaryName.Keyword:
                    this.DivDcAdd.Visible = false;
                    this.DivDcEdit.Visible = false;
                    this.DivExecutorAdd.Visible = false;
                    this.DivExecutorEdit.Visible = false;
                    break;
                case DictionaryName.OtherBu:
                    this.DivDcAdd.Visible = false;
                    this.DivDcEdit.Visible = false;
                    this.DivExecutorAdd.Visible = true;
                    this.DivExecutorEdit.Visible = true;
                    break;
                case DictionaryName.IdeaType:
                    this.DivDcAdd.Visible = true;
                    this.DivDcEdit.Visible = true;
                    this.DivExecutorAdd.Visible = false;
                    this.DivExecutorEdit.Visible = false;
                    break;
                case DictionaryName.NextAction:
                    this.DivDcAdd.Visible = false;
                    this.DivDcEdit.Visible = false;
                    this.DivExecutorAdd.Visible = false;
                    this.DivExecutorEdit.Visible = false;
                    break;
                case DictionaryName.TopicType:
                    this.DivDcAdd.Visible = false;
                    this.DivDcEdit.Visible = false;
                    this.DivExecutorAdd.Visible = false;
                    this.DivExecutorEdit.Visible = false;
                    break;
                default:
                    this.DivDcAdd.Visible = false;
                    this.DivDcEdit.Visible = false;
                    this.DivExecutorAdd.Visible = false;
                    this.DivExecutorEdit.Visible = false;
                    break;
            }
        }

        protected void DdlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ViewState["DicName"] = Convert.ToInt32(this.DdlType.SelectedValue);

            this.BindData();
        }

        protected void GvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)    // 数据行
            {
                // 设置行号
                ((Literal)(e.Row.FindControl("LtrNo"))).Text = String.Format("{0}", e.Row.DataItemIndex + 1);

                DataDictionaryEntry dde = (DataDictionaryEntry)e.Row.DataItem;
            }
        }

        protected void GvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "OpenEdit")
            {
                DataDictionaryEntry dde = null;

                switch ((DictionaryName)this.ViewState["DicName"])
                {
                    case DictionaryName.Keyword:
                        dde = KeywordDicMgr.GetByCode(e.CommandArgument.ToString());
                        this.TxtCodeEdit.Text = dde.Code;
                        this.TxtTitleEdit.Text = dde.Title; this.TxtTitleEdit.ReadOnly = true;
                        this.TxtSortEdit.Text = dde.Sort.ToString();
                        this.CbVisibleEdit.Checked = dde.Visible;
                        this.ViewState["Code"] = dde.Code;
                        break;
                    case DictionaryName.OtherBu:
                        OtherBu obu = OtherBuDicMgr.GetByCode(e.CommandArgument.ToString());
                        this.TxtCodeEdit.Text = obu.Code;
                        this.TxtTitleEdit.Text = obu.Title; this.TxtTitleEdit.ReadOnly = false;
                        this.TxtSortEdit.Text = obu.Sort.ToString();
                        this.CbVisibleEdit.Checked = obu.Visible;
                        this.ViewState["Code"] = obu.Code;
                        this.TxtExecutorEdit.Text = obu.Executor;
                        break;
                    case DictionaryName.IdeaType:
                        IdeaType ite = IdeaTypeDicMgr.GetByCode(e.CommandArgument.ToString());
                        this.TxtCodeEdit.Text = ite.Code;
                        this.TxtTitleEdit.Text = ite.Title; this.TxtTitleEdit.ReadOnly = false;
                        this.TxtSortEdit.Text = ite.Sort.ToString();
                        this.CbVisibleEdit.Checked = ite.Visible;
                        this.ViewState["Code"] = ite.Code;
                        this.TxtDcEdit.Text = ite.DC;
                        break;
                    case DictionaryName.NextAction:
                        dde = NextActionDicMgr.GetByCode(e.CommandArgument.ToString());
                        this.TxtCodeEdit.Text = dde.Code;
                        this.TxtTitleEdit.Text = dde.Title; this.TxtTitleEdit.ReadOnly = false;
                        this.TxtSortEdit.Text = dde.Sort.ToString();
                        this.CbVisibleEdit.Checked = dde.Visible;
                        this.ViewState["Code"] = dde.Code;
                        break;
                    case DictionaryName.TopicType:
                        dde = TopicTypeDicMgr.GetByCode(e.CommandArgument.ToString());
                        this.TxtCodeEdit.Text = dde.Code;
                        this.TxtTitleEdit.Text = dde.Title; this.TxtTitleEdit.ReadOnly = true;
                        this.TxtSortEdit.Text = dde.Sort.ToString();
                        this.CbVisibleEdit.Checked = dde.Visible;
                        this.ViewState["Code"] = dde.Code;
                        break;
                }

                this.MpeEdit.Show();
            }
        }


        protected void BtnSaveAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DataDictionaryEntry dde = null;

                switch ((DictionaryName)this.ViewState["DicName"])
                {
                    case DictionaryName.Keyword:
                        dde = KeywordDicMgr.GetInstance().CreateEntry(
                            this.TxtCodeAdd.Text, this.TxtTitleAdd.Text,
                            Convert.ToInt32(this.TxtSortAdd.Text),
                            true, UserHelper.UserName, DateTime.Now);
                        KeywordDicMgr.GetInstance().Add(dde);
                        break;
                    case DictionaryName.OtherBu:
                        OtherBu obu = OtherBuDicMgr.GetInstance().CreateEntry(
                            this.TxtCodeAdd.Text, this.TxtTitleAdd.Text,
                            Convert.ToInt32(this.TxtSortAdd.Text),
                            true, UserHelper.UserName, DateTime.Now,
                            this.TxtExecutorAdd.Text.Trim().ToLower());

                        if (new BLL.Sys.UserBl().GetUserByItCode(obu.Executor) == null)
                        {
                            MessageHelper.RegShowJSAjax(this.BtnSaveAdd, "Invalid Executor!", this.Page);
                            return;
                        }

                        OtherBuDicMgr.GetInstance().Add(obu);
                        break;
                    case DictionaryName.IdeaType:
                        IdeaType ite = IdeaTypeDicMgr.GetInstance().CreateEntry(
                            this.TxtCodeAdd.Text, this.TxtTitleAdd.Text,
                            Convert.ToInt32(this.TxtSortAdd.Text),
                            true, UserHelper.UserName, DateTime.Now,
                            this.TxtDcAdd.Text.Trim().ToLower());

                        if (new BLL.Sys.UserBl().GetUserByItCode(ite.DC) == null)
                        {
                            MessageHelper.RegShowJSAjax(this.BtnSaveAdd, "Invalid DC!", this.Page);
                            return;
                        }

                        IdeaTypeDicMgr.GetInstance().Add(ite);
                        break;
                    case DictionaryName.NextAction:
                        dde = NextActionDicMgr.GetInstance().CreateEntry(
                            this.TxtCodeAdd.Text, this.TxtTitleAdd.Text,
                            Convert.ToInt32(this.TxtSortAdd.Text),
                            true, UserHelper.UserName, DateTime.Now);

                        NextActionDicMgr.GetInstance().Add(dde);
                        break;
                    case DictionaryName.TopicType:
                        dde = TopicTypeDicMgr.GetInstance().CreateEntry(
                            this.TxtCodeAdd.Text, this.TxtTitleAdd.Text,
                            Convert.ToInt32(this.TxtSortAdd.Text),
                            true, UserHelper.UserName, DateTime.Now);
                        TopicTypeDicMgr.GetInstance().Add(dde);
                        break;
                }

                this.TxtCodeAdd.Text = "";
                this.TxtTitleAdd.Text = "";
                this.TxtSortAdd.Text = "";

                this.TxtDcAdd.Text = "";
                this.TxtExecutorAdd.Text = "";

                MessageHelper.RegShowJSAjax(this.BtnSaveAdd, "Success!", this.Page);
            }
            catch (ApplicationException aex)
            {
                MessageHelper.RegShowJSAjax(this.BtnSaveAdd, aex.Message, this.Page);
            }
            catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
            {
                ErrorHelper.ExceptionHanderForAjax(ex, this.BtnSaveAdd, this.Page);
            }

            this.BindData();
            this.MpeAdd.Hide();
        }

        protected void BtnSaveEdit_Click(object sender, EventArgs e)
        {
            try
            {
                DataDictionaryEntry dde = null;

                switch ((DictionaryName)this.ViewState["DicName"])
                {
                    case DictionaryName.Keyword:
                        dde = KeywordDicMgr.GetByCode(this.ViewState["Code"].ToString());
                        dde.Title = this.TxtTitleEdit.Text;
                        dde.Sort = Convert.ToInt32(this.TxtSortEdit.Text);
                        dde.Visible = this.CbVisibleEdit.Checked;

                        KeywordDicMgr.GetInstance().Update(dde);
                        break;
                    case DictionaryName.OtherBu:
                        string executor = this.TxtExecutorEdit.Text.Trim().ToLower();
                        if (new BLL.Sys.UserBl().GetUserByItCode(executor) == null)
                        {
                            MessageHelper.RegShowJSAjax(this.BtnSaveEdit, "Invalid Executor!", this.Page);
                            return;
                        }

                        OtherBu obu = OtherBuDicMgr.GetByCode(this.ViewState["Code"].ToString());
                        obu.Title = this.TxtTitleEdit.Text;
                        obu.Sort = Convert.ToInt32(this.TxtSortEdit.Text);
                        obu.Visible = this.CbVisibleEdit.Checked;
                        obu.Executor = executor;

                        OtherBuDicMgr.GetInstance().Update(obu);
                        break;
                    case DictionaryName.IdeaType:
                        string dc = this.TxtDcEdit.Text.Trim().ToLower();
                        if (new BLL.Sys.UserBl().GetUserByItCode(dc) == null)
                        {
                            MessageHelper.RegShowJSAjax(this.BtnSaveEdit, "Invalid DC!", this.Page);
                            return;
                        }

                        IdeaType ite = IdeaTypeDicMgr.GetByCode(this.ViewState["Code"].ToString());
                        ite.Title = this.TxtTitleEdit.Text;
                        ite.Sort = Convert.ToInt32(this.TxtSortEdit.Text);
                        ite.Visible = this.CbVisibleEdit.Checked;
                        ite.DC = dc;

                        IdeaTypeDicMgr.GetInstance().Update(ite);
                        break;

                    case DictionaryName.NextAction:
                        dde = NextActionDicMgr.GetByCode(this.ViewState["Code"].ToString());
                        dde.Title = this.TxtTitleEdit.Text;
                        dde.Sort = Convert.ToInt32(this.TxtSortEdit.Text);
                        dde.Visible = this.CbVisibleEdit.Checked;

                        NextActionDicMgr.GetInstance().Update(dde);
                        break;
                    case DictionaryName.TopicType:
                        dde = TopicTypeDicMgr.GetByCode(this.ViewState["Code"].ToString());
                        dde.Title = this.TxtTitleEdit.Text;
                        dde.Sort = Convert.ToInt32(this.TxtSortEdit.Text);
                        dde.Visible = this.CbVisibleEdit.Checked;

                        TopicTypeDicMgr.GetInstance().Update(dde);
                        break;
                }

                MessageHelper.RegShowJSAjax(this.BtnSaveEdit, "Success!", this.Page);
            }
            catch (ApplicationException aex)
            {
                MessageHelper.RegShowJSAjax(this.BtnSaveEdit, aex.Message, this.Page);
            }
            catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
            {
                ErrorHelper.ExceptionHanderForAjax(ex, this.BtnSaveEdit, this.Page);
            }

            this.BindData();
            this.MpeEdit.Hide();
        }
    }
}